$(function() {
	// Waves初始化
	Waves.displayEffect();
	// 输入框获取焦点后出现下划线
	$('.form-control').focus(function() {
		$(this).parent().addClass('fg-toggled');
	}).blur(function() {
		$(this).parent().removeClass('fg-toggled');
	});
});
Checkbix.init();
$(function() {
	// 点击登录按钮
	$('#login-bt').click(function() {
		login();
	});
	// 回车事件
	$('#username, #password').keypress(function (event) {
		if (13 == event.keyCode) {
			login();
		}
	});
});
$(function () {
	if ($('#rememberMe').is(':checked')) {
		$('#adminAccount').val($.cookie("adminAccount"));
		$('#adminPassword').val($.cookie("adminPassword"));
	}
})
// 登录
function login() {
	const adminAccount = $('#adminAccount').val();
	const adminPassword = $('#adminPassword').val();
	const formData = new FormData();
	formData.append("adminAccount", adminAccount);
	formData.append("adminPassword", adminPassword);
	$.ajax({
		url: "/api/admin/v/login",
		type: 'POST',
		data: formData,
		dataType: "text",
		processData: false,
		contentType: false,
		beforeSend: function() {

		},
		success: function(json){
			json = JSON.parse(json);
			if (json.success) {
				const rememberMe = $('#rememberMe').is(':checked');
				$.cookie("adminAccount", adminAccount);
				if (rememberMe) {
					$.cookie("adminPassword", adminPassword);
				}
				location.href="/adminIndex";
			} else {
				alert(json.msg);
				if (json.msg.indexOf("账户")>0) {
					$('#adminAccount').focus();
				}
				if (json.msg.indexOf("密码")>0) {
					$('#adminPassword').focus();
				}
			}
		},
		error: function(error){
			console.log(error);
		}
	});
}